home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / prolog_2.zip / PUZZLES.ZIP / COUSIN.PRO < prev    next >
Text File  |  1987-05-30  |  4KB  |  112 lines

  1. /* Cousin.PRO to find Nth cousins Mth removed in a family tree */
  2.  
  3. /**************************************************************
  4.  *                                                            *
  5.  * Programming assignment #2 ----> Using Prolog               *
  6.  *                                                            *
  7.  * Name : John Kai-Yui Sun                                    *
  8.  *                                                            *
  9.  * SSNO : 568-85-6292                                         *
  10.  *                                                            *
  11.  * Subj : CS 380-01 Programming Languages                     *
  12.  *                                                            *
  13.  * Date : Nov 22, 1986                                        *
  14.  *                                                            *
  15.  * Inst : Dr. John V. Erhart                                  *
  16.  *                                                            *
  17.  **************************************************************
  18.  
  19.  It is easy to characterize cousins: two people are  Nth cousins
  20.  Mth removed (M ≥ 0) if and only if the ancestor  of  one  N + 1
  21.  generations past is the same as the ancestor of other N + 1 + M
  22.  generations past and they have no common ancestors in the  past
  23.  N generations.
  24.  
  25. */
  26.     /* Rules */
  27.  
  28.     sibling(Person_X, Person_Y)            :- 
  29.         parent_child(Person_Z, Person_X),
  30.         parent_child(Person_Z, Person_Y),
  31.         Person_X \= Person_Y.
  32.  
  33.     ancestor(Person_X, Person_Y, 0)        :- sibling(Person_X, Person_Y).
  34.  
  35.     ancestor(Person_X, Person_Y, 1)        :- parent_child(Person_X, Person_Y).
  36.  
  37.     ancestor(Person_X, Person_Y, N_Levels) :-
  38.         N_Levels_minus_1 is N_Levels - 1,
  39.         N_Levels_minus_1 > 0,       
  40.         ancestor(Person_X, Person_Z, N_Levels_minus_1),
  41.         parent_child(Person_Z, Person_Y).
  42.  
  43.     is_cousin(X, Y, N)                     :-
  44.         N > 0,
  45.         sibling(V, W),
  46.         ancestor(W, X, N),
  47.         ancestor(V, Y, N).
  48.  
  49.     find_cousin(X, Y, N, 0)                :- 
  50.         is_cousin(X, Y, N).
  51.  
  52.     find_cousin(X, Y, N, 0)                :- !.
  53.  
  54.     find_cousin(X, Y, N, M)                :-
  55.         is_cousin(X, W, N),
  56.         ancestor(W, Y, M).
  57.            
  58.     find_cousin(X, Y, N, M)                :-
  59.         is_cousin(Y, W, N),
  60.         ancestor(W, X, M).
  61.  
  62.     /* Facts */
  63.  
  64.     parent_child(charles, tom).
  65.     parent_child(charles, mary).
  66.     parent_child(charles, max).
  67.     parent_child(tom, bill).
  68.     parent_child(tom, sue).
  69.     parent_child(mary, frankie).
  70.     parent_child(mary, marie).
  71.     parent_child(max, judy).
  72.     parent_child(max, jane).
  73.     parent_child(bill, larry).
  74.     parent_child(bill, edith).
  75.     parent_child(sue, joe).
  76.     parent_child(sue, tammy).
  77.     parent_child(frankie, cyrus).
  78.     parent_child(marie, paul).
  79.     parent_child(marie, ida).
  80.     parent_child(judy, suzy).
  81.     parent_child(judy, sammy).
  82.     parent_child(larry, dave).
  83.     parent_child(larry, gazina).
  84.     parent_child(joe, adam).
  85.     parent_child(tammy, vidas).
  86.     parent_child(tammy, rosetta).
  87.     parent_child(cyrus, lela).
  88.     parent_child(paul, irene).
  89.     parent_child(suzy, vince).
  90.     parent_child(gazina, betty).
  91.     parent_child(vidas, jesse).
  92.     parent_child(vidas, ruth).
  93.     parent_child(rosetta, edward).
  94.     parent_child(vince, leroy).
  95.     parent_child(vince, alex).
  96.     parent_child(betty, barbara).
  97.     parent_child(betty, bernice).
  98.     parent_child(jesse, james).
  99.     parent_child(jesse, annie).
  100.     parent_child(edward, shelly).
  101.     parent_child(leroy, agnes).
  102.     parent_child(barbara, jason).
  103.     parent_child(james, frank).
  104.     parent_child(annie, fran).
  105.     parent_child(annie, ollie).
  106.     parent_child(shelly, robert).
  107.     parent_child(agnes, tim).
  108.     parent_child(agnes, ralph).
  109.     parent_child(ralph, paula).
  110.  
  111.  
  112.